{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6a9c2b31",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "from tsfresh import (\n",
    "    extract_features,\n",
    "    extract_relevant_features,\n",
    "    select_features,\n",
    ")\n",
    "from tsfresh.utilities.dataframe_functions import impute"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0efd823d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load data\n",
    "\n",
    "X = pd.read_csv(\"occupancy.csv\", parse_dates=[\"date\"])\n",
    "y = pd.read_csv(\"occupancy_target.csv\", index_col=\"id\")[\"occupancy\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "beb73878",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Feature Extraction: 100%|██████████████████████████████████████████████████████████████| 10/10 [00:15<00:00,  1.59s/it]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(135, 789)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# create features for one time series: light\n",
    "\n",
    "features = extract_features(\n",
    "    X[[\"id\", \"light\"]],\n",
    "    column_id=\"id\",\n",
    "    impute_function=impute,\n",
    ")\n",
    "\n",
    "features.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7a323178",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((135, 789), (135,))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# features and target have the same shape\n",
    "\n",
    "features.shape, y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c6a2f3bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "135"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# select relevant features\n",
    "\n",
    "features = select_features(features, y)\n",
    "\n",
    "len(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ba30468f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>light__minimum</th>\n",
       "      <th>light__agg_linear_trend__attr_\"intercept\"__chunk_len_50__f_agg_\"min\"</th>\n",
       "      <th>light__quantile__q_0.1</th>\n",
       "      <th>light__quantile__q_0.3</th>\n",
       "      <th>light__quantile__q_0.4</th>\n",
       "      <th>light__quantile__q_0.2</th>\n",
       "      <th>light__median</th>\n",
       "      <th>light__quantile__q_0.6</th>\n",
       "      <th>light__agg_linear_trend__attr_\"intercept\"__chunk_len_10__f_agg_\"min\"</th>\n",
       "      <th>light__quantile__q_0.7</th>\n",
       "      <th>...</th>\n",
       "      <th>light__index_mass_quantile__q_0.2</th>\n",
       "      <th>light__ratio_beyond_r_sigma__r_2.5</th>\n",
       "      <th>light__index_mass_quantile__q_0.8</th>\n",
       "      <th>light__agg_autocorrelation__f_agg_\"mean\"__maxlag_40</th>\n",
       "      <th>light__cwt_coefficients__coeff_11__w_2__widths_(2, 5, 10, 20)</th>\n",
       "      <th>light__index_mass_quantile__q_0.6</th>\n",
       "      <th>light__index_mass_quantile__q_0.7</th>\n",
       "      <th>light__index_mass_quantile__q_0.3</th>\n",
       "      <th>light__agg_autocorrelation__f_agg_\"median\"__maxlag_40</th>\n",
       "      <th>light__ar_coefficient__coeff_2__k_10</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.033333</td>\n",
       "      <td>0.116667</td>\n",
       "      <td>0.1</td>\n",
       "      <td>-0.010369</td>\n",
       "      <td>-137.454032</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>0.05</td>\n",
       "      <td>-0.068578</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.600000</td>\n",
       "      <td>0.700000</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.600000</td>\n",
       "      <td>0.700000</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.600000</td>\n",
       "      <td>0.700000</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.600000</td>\n",
       "      <td>0.700000</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 277 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   light__minimum  \\\n",
       "1             0.0   \n",
       "2             0.0   \n",
       "3             0.0   \n",
       "4             0.0   \n",
       "5             0.0   \n",
       "\n",
       "   light__agg_linear_trend__attr_\"intercept\"__chunk_len_50__f_agg_\"min\"  \\\n",
       "1                                                0.0                      \n",
       "2                                                0.0                      \n",
       "3                                                0.0                      \n",
       "4                                                0.0                      \n",
       "5                                                0.0                      \n",
       "\n",
       "   light__quantile__q_0.1  light__quantile__q_0.3  light__quantile__q_0.4  \\\n",
       "1                     0.0                     0.0                     0.0   \n",
       "2                     0.0                     0.0                     0.0   \n",
       "3                     0.0                     0.0                     0.0   \n",
       "4                     0.0                     0.0                     0.0   \n",
       "5                     0.0                     0.0                     0.0   \n",
       "\n",
       "   light__quantile__q_0.2  light__median  light__quantile__q_0.6  \\\n",
       "1                     0.0            0.0                     0.0   \n",
       "2                     0.0            0.0                     0.0   \n",
       "3                     0.0            0.0                     0.0   \n",
       "4                     0.0            0.0                     0.0   \n",
       "5                     0.0            0.0                     0.0   \n",
       "\n",
       "   light__agg_linear_trend__attr_\"intercept\"__chunk_len_10__f_agg_\"min\"  \\\n",
       "1                                                0.0                      \n",
       "2                                                0.0                      \n",
       "3                                                0.0                      \n",
       "4                                                0.0                      \n",
       "5                                                0.0                      \n",
       "\n",
       "   light__quantile__q_0.7  ...  light__index_mass_quantile__q_0.2  \\\n",
       "1                     0.0  ...                           0.033333   \n",
       "2                     0.0  ...                           0.200000   \n",
       "3                     0.0  ...                           0.200000   \n",
       "4                     0.0  ...                           0.200000   \n",
       "5                     0.0  ...                           0.200000   \n",
       "\n",
       "   light__ratio_beyond_r_sigma__r_2.5  light__index_mass_quantile__q_0.8  \\\n",
       "1                            0.116667                                0.1   \n",
       "2                            0.000000                                0.8   \n",
       "3                            0.000000                                0.8   \n",
       "4                            0.000000                                0.8   \n",
       "5                            0.000000                                0.8   \n",
       "\n",
       "   light__agg_autocorrelation__f_agg_\"mean\"__maxlag_40  \\\n",
       "1                                          -0.010369     \n",
       "2                                           0.000000     \n",
       "3                                           0.000000     \n",
       "4                                           0.000000     \n",
       "5                                           0.000000     \n",
       "\n",
       "   light__cwt_coefficients__coeff_11__w_2__widths_(2, 5, 10, 20)  \\\n",
       "1                                        -137.454032               \n",
       "2                                           0.000000               \n",
       "3                                           0.000000               \n",
       "4                                           0.000000               \n",
       "5                                           0.000000               \n",
       "\n",
       "   light__index_mass_quantile__q_0.6  light__index_mass_quantile__q_0.7  \\\n",
       "1                           0.083333                           0.083333   \n",
       "2                           0.600000                           0.700000   \n",
       "3                           0.600000                           0.700000   \n",
       "4                           0.600000                           0.700000   \n",
       "5                           0.600000                           0.700000   \n",
       "\n",
       "   light__index_mass_quantile__q_0.3  \\\n",
       "1                               0.05   \n",
       "2                               0.30   \n",
       "3                               0.30   \n",
       "4                               0.30   \n",
       "5                               0.30   \n",
       "\n",
       "   light__agg_autocorrelation__f_agg_\"median\"__maxlag_40  \\\n",
       "1                                          -0.068578       \n",
       "2                                           0.000000       \n",
       "3                                           0.000000       \n",
       "4                                           0.000000       \n",
       "5                                           0.000000       \n",
       "\n",
       "   light__ar_coefficient__coeff_2__k_10  \n",
       "1                                   0.0  \n",
       "2                                   0.0  \n",
       "3                                   0.0  \n",
       "4                                   0.0  \n",
       "5                                   0.0  \n",
       "\n",
       "[5 rows x 277 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "df7decb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['light__minimum',\n",
       "       'light__agg_linear_trend__attr_\"intercept\"__chunk_len_50__f_agg_\"min\"',\n",
       "       'light__quantile__q_0.1', 'light__quantile__q_0.3',\n",
       "       'light__quantile__q_0.4'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# select 5 features to display in book recipe\n",
    "\n",
    "feats = features.columns[0:5]\n",
    "\n",
    "feats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b415e875",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>light__minimum</th>\n",
       "      <th>light__agg_linear_trend__attr_\"intercept\"__chunk_len_50__f_agg_\"min\"</th>\n",
       "      <th>light__quantile__q_0.1</th>\n",
       "      <th>light__quantile__q_0.3</th>\n",
       "      <th>light__quantile__q_0.4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   light__minimum  \\\n",
       "1             0.0   \n",
       "2             0.0   \n",
       "3             0.0   \n",
       "4             0.0   \n",
       "5             0.0   \n",
       "\n",
       "   light__agg_linear_trend__attr_\"intercept\"__chunk_len_50__f_agg_\"min\"  \\\n",
       "1                                                0.0                      \n",
       "2                                                0.0                      \n",
       "3                                                0.0                      \n",
       "4                                                0.0                      \n",
       "5                                                0.0                      \n",
       "\n",
       "   light__quantile__q_0.1  light__quantile__q_0.3  light__quantile__q_0.4  \n",
       "1                     0.0                     0.0                     0.0  \n",
       "2                     0.0                     0.0                     0.0  \n",
       "3                     0.0                     0.0                     0.0  \n",
       "4                     0.0                     0.0                     0.0  \n",
       "5                     0.0                     0.0                     0.0  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# display some features (for book)\n",
    "\n",
    "features[feats].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ecb48d08",
   "metadata": {},
   "outputs": [],
   "source": [
    "# split into train and test\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    features,\n",
    "    y,\n",
    "    test_size=0.1,\n",
    "    random_state=42,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b1031600",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      0.91      0.95        11\n",
      "           1       0.75      1.00      0.86         3\n",
      "\n",
      "    accuracy                           0.93        14\n",
      "   macro avg       0.88      0.95      0.90        14\n",
      "weighted avg       0.95      0.93      0.93        14\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# train and evaluate logistic regression\n",
    "\n",
    "cls = LogisticRegression(random_state=10, C=0.1, max_iter=1000)\n",
    "cls.fit(X_train, y_train)\n",
    "\n",
    "print(classification_report(y_test, cls.predict(X_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1c75dba8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Feature Extraction: 100%|██████████████████████████████████████████████████████████████| 10/10 [00:53<00:00,  5.32s/it]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(135, 968)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# create and select features for all time series\n",
    "# automatically\n",
    "\n",
    "features = extract_relevant_features(\n",
    "    X,\n",
    "    y,\n",
    "    column_id=\"id\",\n",
    "    column_sort=\"date\",\n",
    ")\n",
    "\n",
    "features.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "28ec65f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# split into train and test\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    features,\n",
    "    y,\n",
    "    test_size=0.1,\n",
    "    random_state=42,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7e07486e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      0.91      0.95        11\n",
      "           1       0.75      1.00      0.86         3\n",
      "\n",
      "    accuracy                           0.93        14\n",
      "   macro avg       0.88      0.95      0.90        14\n",
      "weighted avg       0.95      0.93      0.93        14\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# train and evaluate logistic regression\n",
    "\n",
    "cls = LogisticRegression(random_state=10, C=0.000000000000001)\n",
    "cls.fit(X_train, y_train)\n",
    "\n",
    "print(classification_report(y_test, cls.predict(X_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af806023",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fsml",
   "language": "python",
   "name": "fsml"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
